<?php
namespace app\common\model;

use think\Db;
use think\Model;


class Users extends Model
{

    
    protected $dateFormat = 'Y-m-d H:i';

    public function profile()
    {
        return $this->hasOne('users', 'user_id', 'proxy_id')->field('nick_name');
    }

    protected function getUserIdAttr($value, $data)
    {
        return hashid($data['user_id'] ?? 0);
    }

    public function getDetail($where = [])
    {
        $join = [
            ['user_extend e','e.user_id = a.user_id','left']
        ];
        $field = 'e.noble_id,e.noble_time,a.user_id,a.header_img,a.nick_name,a.sex,a.uuid,e.grade,a.ID,a.phone,e.wx_name,e.qq_name,a.money,a.cash,a.coin,a.sign,a.age,e.fans_num,a.birthday,a.constellation';

        $data =  $this->alias('a')
            ->join($join)
            ->where($where)
            ->field($field)
            ->find();
        $data['headWear'] = \app\api\controller\Base::getHeadWear($data->getData('user_id'));
        return $data;
    }

    /**
     * Created by xiaosong
     * E-mail:306027376@qq.com
     * 编辑个人资料页面返显
     */
    public function getInfo($user_id = 0)
    {

        $map['user_id'] = $user_id;
        $map['status']  = 1;
        return $this->where($map)
            ->field('header_img,nick_name,sex,sign,uuid,birthday,constellation,bg')
            ->find();
    }




    /**
     * 后台获取用户列表
     */
    public function userList($where = [])
    {
        return $this
            ->where($where)
            ->order('user_id desc')
            ->paginate('', false, ['query' => request()->param()]);
    }


    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 家族申请列表获取用户  房间管理获取用户
     */
    public function getItems($map = [])
    {
        $join = [
            ['user_extend e','e.user_id = a.user_id','left']
        ];

        $field = 'a.user_id,a.header_img,a.sex,a.nick_name,e.noble_id,e.noble_time,e.level,a.sign';

        $rows =  $this->alias('a')
            ->where($map)
            ->join($join)
            ->field($field)
            ->order('e.online_status desc')
            ->cache(1)
            ->paginate()->each(function ($item){
                $item['noble_id'] = \app\api\controller\Base::checkNoble($item);
                $item['headWear'] = \app\api\controller\Base::getHeadWear($item->getData('user_id'));
//                $item['user_id']  = hashid($item['user_id']);

            });
        return ['thisPage'=>$rows->currentPage(),'hasNext'=>$rows->hasMore(),'data'=>$rows->items()];
    }

    public function getNoble($map = [])
    {
        $join = [
            ['user_extend e','e.user_id = a.user_id','left']
        ];

        $field = 'a.user_id,a.header_img,a.nick_name,a.sign,e.noble_id,e.noble_time,e.online_time,e.online_status';

        $rows =  $this->alias('a')
            ->where($map)
            ->join($join)
            ->field($field)
            ->order(['e.noble_id'=>'desc','noble_time'=>'desc'])
            ->cache(15)
            ->paginate()->each(function ($item){
                $item['noble_id'] = \app\api\controller\Base::checkNoble($item);

                if ($item['online_status']){
                    $item['status'] = '当前在线';
                }else{
                    $item['status'] = formatTime($item['online_time']);
                }

//                $item['user_id'] = hashid($item['user_id']);
            });
        return ['thisPage'=>$rows->currentPage(),'hasNext'=>$rows->hasMore(),'data'=>$rows->items()];
    }



    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 广场获取
     */
    public function rows($map = [],$user_id = 0)
    {
        $join = [
            ['skill_apply s','s.user_id = a.user_id','left'],
            ['user_extend e','e.user_id = a.user_id','left'],
            ['skill sk','s.skill_id = sk.skill_id','left']
        ];

//        $_empty = [
//            'apply_id' => 0,
//            'skill_name' => '',
//            'num' => 0
//        ];

        $field = 'a.user_id,a.header_img,a.sex,a.nick_name,a.sign,e.online_time,e.online_status,e.room_id,e.noble_id,e.noble_time,e.place,e.level,s.apply_id,sk.skill_name,s.num,s.is_use,s.status,s.update_time';
//        $field = 'a.user_id,a.header_img,a.sex,a.nick_name,a.sign,e.online_time,e.online_status,e.room_id,e.noble_id,e.noble_time,e.level';
//        $skill = [];

        //存在技能id筛选有技能的用户  不用处理技能信息
        if (isset($map['s.skill_id'])){
            $check = false;

        }else{
            $check = true;
        }

        $rows =  $this->alias('a')->where($map)->join($join)
            ->field($field)
            ->order('e.online_status desc,s.update_time desc')
            ->cache(15)
            ->paginate();

        $items   = $rows->items();
//        $array   = [];
        $key_arr = [];
        foreach ($items as $k => $item){

            if ($item['user_id'] == $user_id){
                unset($items[$k]);
                continue;
            }

            if (in_array($item['user_id'],$key_arr)) {
                unset($items[$k]);
                continue;
            } else {
                $key_arr[] = $item['user_id'];
            }


            if ($check){
                if ($item['is_use'] != 1 || $item['status'] != 1){
                    $items[$k]['apply_id']   = 0;
                    $items[$k]['skill_name'] = '';
                    $items[$k]['num']        = 0;
                    $items[$k]['is_use']     = 0;
                }
            }



            if ($item['online_status']){
                $items[$k]['status'] = '当前在线';
            }else{
                $items[$k]['status'] = formatTime($item['online_time']);
            }

            $items[$k]['noble_id'] = \app\api\controller\Base::checkNoble($item);
            $items[$k]['noble_name'] = getNobleName($item['noble_id']);
//            $items[$k]['user_id'] = hashid($item['user_id']);

        }

//
//            ->each(function ($item)use ($check){
//
//                if ($check){
//                    if ($item['is_use'] != 1 || $item['status'] != 1){
//                        $item['apply_id']   = 0;
//                        $item['skill_name'] = '';
//                        $item['num']        = 0;
//                        $item['is_use']     = 0;
//                    }
//                }
//
//
//                if ($item['online_status']){
//
//                    $item['status'] = '当前在线';
//
//                }else{
//
//                    $item['status'] = formatTime($item['online_time']);
//
//                }
//                $item['noble_id'] = \app\api\controller\Base::checkNoble($item);
//                $item['noble_name'] = getNobleName($item['noble_id']);
////                if ($item['room_id']){
////                    $item['skill'] = $_empty;
////                }else{
////
////                    //用户不在房间中   查询是否有技能
////                    $skill['a.user_id'] = $item['user_id'];
////                    $skill['a.status']  = 1;
////                    $skill['a.is_use']  = 1;
////
////                    $item['skill'] = Db::name('skill_apply')
////                        ->alias('a')
////                        ->join( [
////                            ['skill s','s.skill_id = a.skill_id','left']
////                        ])
////                        ->where($skill)
////                        ->field('a.apply_id,s.skill_name,a.num')
////                        ->find()??$_empty;
////                }
//
//                $item['user_id'] = hashid($item['user_id']);
//
//            });

        return ['thisPage'=>$rows->currentPage(),'hasNext'=>$rows->hasMore(),'data'=>array_values($items)];
    }

    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     */
    public function search($map = [],$user_id = 0)
    {
        $join = [
            ['user_extend e','e.user_id = a.user_id','left'],
        ];

        $field = "a.user_id,a.uuid,a.nick_name,a.header_img,a.sex,a.sign,a.birthday,a.constellation";
        $items =  $this->alias('a')->where($map)->join($join)
            ->field($field)
//            ->cache(30)
            ->limit(30)
            ->select();


        $where['user_id'] = $user_id;
        $where['status']  = 1;

        foreach ($items as $k => $v){
            $items[$k]['age'] = getAge($v['birthday']);
            $where['follow_user']   = $v['user_id'];
            $items[$k]['is_follow'] = Db::name('user_follow')->where($where)->value('follow_id')?1:0;
//            $items[$k]['user_id']   = hashid($v['user_id']);
        }

        return $items;
    }

    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     */
    public function latSearch($map = [],$user_id = 0,$distance = false,$userExtra = [])
    {
        $max  = Db::name('extend')->where('id',1)->cache(60)->value('distance');

        $join = [
            ['user_extend e','e.user_id = a.user_id','left'],
        ];

        $log = $userExtra['log'] ?? 0;
        $lat = $userExtra['lat'] ?? 0;

        $field = "e.place,(st_distance (point (e.log,e.lat),point($log,$lat) ) / 0.0111) AS distance,e.log,e.lat,a.user_id,a.uuid,a.nick_name,a.header_img,a.sex,a.sign,a.age";
        $items =  $this->alias('a')->where($map)->join($join)
            ->field($field)
            ->order('distance')
            ->having("distance < $max")
            ->cache(30)
            ->limit(30)
            ->select();

        $where['user_id'] = $user_id;
        $where['status']  = 1;

        foreach ($items as $k => $v){

            $where['follow_user']   = $v['user_id'];
            $items[$k]['is_follow'] = Db::name('user_follow')->where($where)->value('follow_id')?1:0;
//            $items[$k]['user_id']   = hashid($v['user_id']);

        }

        return $items;
    }

    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 用户信息列表  上麦请求获取
     */
    public function _list(array $map = []): array
    {

        $join = [
            ['user_extend e','e.user_id = a.user_id','left'],
        ];

        $field = "a.user_id,a.nick_name,a.header_img,e.noble_id,e.level,a.sex,a.birthday";

        $rows = $items =  $this->alias('a')->where($map)->join($join)
            ->field($field)
            ->cache(3)
            ->select();
        foreach ($rows as $k => $v){
            $rows[$k]['age'] = getAge($v['birthday']).'岁';
//            $rows[$k]['user_id'] = hashid($v['user_id']);
        }

        return $rows;

    }


}